//
//  ATLargeImageView.m
//  ShowLageImage
//
//  Created by lianglibao on 2018/3/3.
//  Copyright © 2018年 lianglibao. All rights reserved.
//

#import "ATLargeImageView.h"
#import "ATTiledImageView.h"

#define maxWidth 1920.0
@interface ATLargeImageView ()
@property (nonatomic, strong) ATTiledImageView *ATTiledImageView;

@end

@implementation ATLargeImageView{
    CGSize lastSize;
}

- (ATTiledImageView *)ATTiledImageViewWithParameter:(id)parameter
                                    frame:(CGRect)frame
                                tileCount:(NSInteger)tileCount {
    if (!_ATTiledImageView) {
            _ATTiledImageView = [[ATTiledImageView alloc] initWithImageInfo:parameter
                                                              andFrame:frame
                                                          andTileCount:tileCount];
    } else {
        _ATTiledImageView.imageInfo = parameter;
    }
    return _ATTiledImageView;
}

- (instancetype)initWithImageInfo:(id)imageInfo
                          andTileCount:(NSInteger)tileCount {
    self = [super init];
    if(self){
        _imageInfo = imageInfo;
        _tileCount = tileCount;
        [self selfInit];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.ATTiledImageView.frame = self.bounds;
    // ATTiledImageView1.frame = self.bounds;
    // [ATTiledImageView1 setNeedsDisplay];
}

- (void)selfInit {
    @autoreleasepool {
        if (!_imageInfo) {
            return;
        }

        CGSize originImageSize = CGSizeZero;
        if ([_imageInfo isKindOfClass:NSString.class]) {
            originImageSize = [UIImage imageWithContentsOfFile:_imageInfo].size;
        } else {
            if ([_imageInfo isKindOfClass:UIImage.class]) {
                originImageSize = ((UIImage *)_imageInfo).size;
            }
        }

        if (CGSizeEqualToSize(CGSizeZero, originImageSize)) {
            return;
        }
        
        CGRect bounds = [[UIScreen mainScreen] bounds];
        CGSize screenSize = bounds.size;
        CGSize viewSize = CGSizeZero;
        self.maxSize = originImageSize;
        if(originImageSize.width > originImageSize.height) {
            viewSize.width = screenSize.width;
            viewSize.height = screenSize.width / originImageSize.width * originImageSize.height;
            screenSize = viewSize;
        }
        self.frame = CGRectMake(0,
                                0,
                                screenSize.width,
                                screenSize.height
                                );
        lastSize = self.frame.size;
        
        self.ATTiledImageView = [self ATTiledImageViewWithParameter:_imageInfo
                                                    frame:CGRectMake(0,
                                                                     0,
                                                                     screenSize.width,
                                                                     screenSize.height
                                                                     )
                                                tileCount:self.tileCount];
        [self addSubview:self.ATTiledImageView];
        // ATTiledImageView1 *imageView = [[ATTiledImageView1 alloc]initWithImageName:_imageName
        // andFrame:CGRectMake(0,
        // 0,
        // screenSize.width,
        // screenSize.height)
        // andTileCount:self.tileCount];
        // [self addSubview:imageView];
        // ATTiledImageView1 = imageView;
    }
}

- (void)setImageInfo:(id)imageInfo {
    _imageInfo = imageInfo;
    [self selfInit];
}

@end
